/*
 *Copyright © 2018 anji-plus
 *安吉加加信息技术有限公司
 *http://www.anji-plus.com
 *All rights reserved.
 */
package com.ocom.oauth.controller;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.ocom.common.entity.typehandler.PersonInfoHandle;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.oauth.utils.UrlUtil;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;


    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/get")
    public Result get(@RequestBody CaptchaVO data, HttpServletRequest request) {
        assert request.getRemoteHost()!=null;
        data.setBrowserInfo(UrlUtil.getRemoteId(request));
        ResponseModel responseModel =  captchaService.get(data);
        if(responseModel.getRepCode().equals("0000")){
            return ResultUtil.success(responseModel.getRepData());
        }
        return ResultUtil.info(Integer.valueOf(responseModel.getRepCode()),responseModel.getRepMsg());
    }

    @PostMapping("/check")
    public Result check(@RequestBody CaptchaVO data, HttpServletRequest request) {
        data.setBrowserInfo(UrlUtil.getRemoteId(request));
        ResponseModel responseModel =  captchaService.check(data);
        System.out.println(responseModel);
        if(responseModel.getRepCode().equals("0000")){
            String auth = PersonInfoHandle.encryptS(data.getPointJson());
            String ip = request.getHeader("remoteip");
            redisUtil.set("AuthCode:"+ ip +":"+auth,1 ,60l);
            return ResultUtil.success(auth);
        }
        return ResultUtil.info(Integer.valueOf(responseModel.getRepCode()),responseModel.getRepMsg());
    }

    /***
     * 服务端验证接口，独立部署的场景使用，集成部署的场景：服务内部调用，不需要调用此接口可注释掉
     * @param data
     * @param request
     * @return
     */
//    @PostMapping("/verify")
//    public ResponseModel verify(@RequestBody CaptchaVO data, HttpServletRequest request) {
//        return captchaService.verification(data);
//    }



}
